package cn.whx.hrm.interceptor;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
@Component
public class AuthFeignInterceptor implements RequestInterceptor{
    private  static  String url = "http://localhost:3010/oauth/token?client_id=system&client_secret=1&grant_type=client_credentials&redirect_uri=http://www.baidu.com";
    @Override
    public void apply(RequestTemplate template) {
        //发起请求获取token
        String req = HttpUtil.get(url);
        //把json转成java对象
        Map map = JSON.parseObject(req, Map.class);
        //获取这个token
        String accessToken = map.get("access_token").toString();
        //把token设置到请求头中
        template.header("Authorization","bearer "+accessToken);
    }
}
